<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="收支类型">
        <el-radio-group v-model="form.bill_type">
          <el-radio label="income">收入</el-radio>
          <el-radio label="payed">支出</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="费用名称" prop="bill_title">
        <el-input v-model="form.bill_title" placeholder="请输入费用名称" />
      </el-form-item>
      <el-form-item label="账单金额" prop="bill_money">
        <el-input v-model="form.bill_money" placeholder="请输入账单金额" />
      </el-form-item>
      <el-form-item label="日期" prop="bill_date">
        <el-date-picker
          v-model="form.bill_date"
          format="yyyy-MM-dd"
          placeholder="请选择记账日期"
          type="date"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { parseTime } from '@/utils/index'
  export default {
    name: 'DidBill',
    data() {
      return {
        form: {
          bill_type: 'income',
          bill_title: '',
          bill_money: '',
          is_pay: '1',
          bill_date: parseTime(new Date(), '{y}-{m}-{d}'),
        },
        rules: {
          bill_title: [
            { required: true, trigger: 'blur', message: '请输入费用名称' },
          ],
          bill_money: [
            { required: true, trigger: 'blur', message: '请输入账单金额' },
          ],
          bill_date: [
            { required: true, trigger: 'blur', message: '请选择记账日期' },
          ],
        },
        title: '添加记账',
        dialogFormVisible: false,
      }
    },
    methods: {
      async showEdit() {
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        let e = Object.assign({}, this.form)
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.$baseMessage('添加成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data', e)
            this.close()
          }
        })
      },
    },
  }
</script>
